//
//  SNOrderFormListHeaderView.m
//  app
//
//  Created by 王少宁 on 15/5/5.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//  订单列表的 cell 的 headerView

#import "SNOrderFormListHeaderView.h"

@interface SNOrderFormListHeaderView ()
/** 订单号*/
@property (nonatomic, retain) UILabel * snLabel;
/** 订单金额总额(包含运费)*/
@property (nonatomic, retain) UILabel * moneyLabel;
/** 订单创建时间*/
@property (nonatomic, retain) UILabel * createTimeLabel;
/** 订单状态*/
@property (nonatomic, retain) UILabel * statusLabel;
/** 收货人姓名*/
@property (nonatomic, retain) UILabel * nameLabel;
/** 商品总数*/
@property (nonatomic, retain) UILabel * productNumLabel;
/** 删除订单*/
@property (nonatomic, retain) UIButton * deleteOrderBtn;
@property (nonatomic, retain) UIButton * mainBtn;
@end

@implementation SNOrderFormListHeaderView

- (instancetype)init
{
    if ([super init]) {
        self.backgroundColor = [UIColor clearColor];
        [self createHeaderView];
    }
    return self;
}

- (void)createHeaderView
{
    _createTimeLabel = CREATE_WITH_FRAME_OF(UILabel, [self.ubo ox:SCREEN_WIDTH-UF(24)-120 oy:5],
                                            [self.ufo ow:120 oh:20]);
    _createTimeLabel.font = JKAppGlobleFontMinTagText;
    _createTimeLabel.textColor = [XCommon hexStringToColor:kColor_s_light_gray_text];
    _createTimeLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview: _createTimeLabel];
    
    _snLabel = CREATE_WITH_FRAME_OF(UILabel, CGPointMake(UF(24), _createTimeLabel.ufo.y),
                                    [_createTimeLabel.ubo fw:-1.0 ow:SCREEN_WIDTH-UF(48) - 5 fh:1.0]);
    _snLabel.font = JKAppGlobleFontMinTagText;
    _snLabel.textColor = [XCommon hexStringToColor:kColor_s_light_gray_text];
    [self addSubview:_snLabel];
    
    _nameLabel = CREATE_WITH_FRAME_OF(UILabel, [_snLabel.ufo fx:0 fy:1.0 oy:13], [_snLabel.ubo ow:100 fh:1.0]);
    _nameLabel.font = JKAppGlobleFontSmallTagText;
    _nameLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_nameLabel];
    
    _statusLabel = CREATE_WITH_FRAME_OF(UILabel, [_nameLabel.ufo ox:SCREEN_WIDTH - _nameLabel.ufo.x - _nameLabel.ubo.width*0.7 fy:0],
                                        [_nameLabel.ubo fw:0.7 fh:1.0]);
    _statusLabel.font = JKAppGlobleFontContentText;
    _statusLabel.layer.masksToBounds = YES;
    _statusLabel.layer.cornerRadius = 3;
    _statusLabel.layer.borderWidth = UP(2);
    _statusLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_statusLabel];
    
    _moneyLabel = CREATE_WITH_FRAME_OF(UILabel, [_nameLabel.ufo fx:0 fy:1.0 oy:12],
                                       [_nameLabel.ubo fw:1.0 oh:25]);
    _moneyLabel.font = JKAppGlobleFontSmallTagText;
    [self addSubview:_moneyLabel];
    
    _productNumLabel = CREATE_WITH_FRAME_OF(UILabel, [self.ufo ox:_createTimeLabel.ufo.x oy:_moneyLabel.ufo.y],
                                          [self.ubo ow:_createTimeLabel.ubo.width oh:_moneyLabel.ubo.height]);
    _productNumLabel.font = JKAppGlobleFontSmallTagText;
    _productNumLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_productNumLabel];
    
    _mainBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _mainBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 75);
    [_mainBtn addTarget:self action:@selector(mainBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_mainBtn];
    
    _deleteOrderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteOrderBtn.frame = CGRectOf([self.ufo fx:0 ox:_statusLabel.ufo.x-_statusLabel.ubo.height*3+5 oy:_statusLabel.ufo.y-5],
                                     [self.ubo ow:_statusLabel.ubo.height*3 oh:_statusLabel.ubo.height*1.5]);
    [_deleteOrderBtn setBackgroundImage:[UIImage imageNamed:@"ic_order_trashcan.png"] forState:UIControlStateNormal];
    [_deleteOrderBtn addTarget:self action:@selector(deleteOrderBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_deleteOrderBtn];
    
}

- (void)viewWithModel:(SNOrderFormListHeaderModel *)model
{
    _snLabel.text = [NSString stringWithFormat:@"订单号 : %@",model.sn];
    
    NSString * str = [NSString stringWithFormat:@"实付 :￥%@",model.money];
    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attributedStr addAttributes:@{NSFontAttributeName:JKAppGlobleFontMinTagText,NSForegroundColorAttributeName:[XCommon hexStringToColor:kColor_s_light_gray_text]} range:NSMakeRange(0, 4)];
    [attributedStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:[XCommon hexStringToColor:kColor_l_light_gray_text]} range:NSMakeRange(4, str.length-4)];
    _moneyLabel.attributedText = attributedStr;
    
    _createTimeLabel.text = [XCommon timeWithDateline:model.createTime];
    
    _nameLabel.text = model.receiver_name;
    
    _productNumLabel.text = [NSString stringWithFormat:@"共%d件商品",model.productNum];
    
    if ([model.isReturns intValue] == 0) {
        switch ([model.status intValue]) {
            case 0:
                _statusLabel.text = @"未支付";
                _statusLabel.layer.borderColor = [XCommon hexStringToColor:@"#FC3E53"].CGColor;
                _statusLabel.textColor = [XCommon hexStringToColor:@"#FC3E53"];
                break;
            case 1:
                _statusLabel.text = @"支付成功";
                _statusLabel.layer.borderColor = [XCommon hexStringToColor:kColor_main_theme_orange].CGColor;
                _statusLabel.textColor = [XCommon hexStringToColor:kColor_main_theme_orange];
                break;
            case 2:
                _statusLabel.text = @"已发货";
                _statusLabel.layer.borderColor = [XCommon hexStringToColor:kColor_main_theme_orange].CGColor;
                _statusLabel.textColor = [XCommon hexStringToColor:kColor_main_theme_orange];
                break;
            case 4:
                _statusLabel.text = @"已签收";
                _statusLabel.layer.borderColor = [XCommon hexStringToColor:kColor_main_theme_orange].CGColor;
                _statusLabel.textColor = [XCommon hexStringToColor:kColor_main_theme_orange];
                break;
            case 5:
                _statusLabel.text = @"已失效";
                _statusLabel.layer.borderColor = [XCommon hexStringToColor:@"#888888"].CGColor;
                _statusLabel.textColor = [XCommon hexStringToColor:@"#888888"];
                break;
            case 6:
                _statusLabel.text = @"订单取消";
                _statusLabel.layer.borderColor = [XCommon hexStringToColor:@"#888888"].CGColor;
                _statusLabel.textColor = [XCommon hexStringToColor:@"#888888"];
                break;
            case 8:
                _statusLabel.text = @"备货中";
                _statusLabel.layer.borderColor = [XCommon hexStringToColor:@"#888888"].CGColor;
                _statusLabel.textColor = [XCommon hexStringToColor:@"#888888"];
                break;
            case 9:
                _statusLabel.text = @"备货中";
                _statusLabel.layer.borderColor = [XCommon hexStringToColor:@"#888888"].CGColor;
                _statusLabel.textColor = [XCommon hexStringToColor:@"#888888"];
                break;
            default:
                break;
        }
    } else {
        _statusLabel.text = @"售后中";
        _statusLabel.layer.borderColor = [XCommon hexStringToColor:@"#888888"].CGColor;
        _statusLabel.textColor = [XCommon hexStringToColor:@"#888888"];
    }
    
    _deleteOrderBtn.hidden = !(model.status.intValue == 4 || model.status.intValue == 5 || model.status.intValue == 6);
}

- (void)deleteOrderBtnClick
{
    if (self.deletage && [self.deletage respondsToSelector:@selector(deleteOrderBtnClick:)]) {
        [self.deletage performSelector:@selector(deleteOrderBtnClick:) withObject:[NSString stringWithFormat:@"%ld",(long)self.section]];
    }
}

- (void)drawRect:(CGRect)rect
{
    [self drawBackground:CGRectMake(0, 30, SCREEN_WIDTH, rect.size.height-30) color:[UIColor whiteColor]];
    [self drawHorizonLineLeft:0 right:0 at:30 - UP(1)];
    [self drawHorizonLineLeft:UF(24) right:0 at:66 - UP(1)];
}

#pragma mark - 点击了 headerView
- (void)mainBtnClick
{
    if ([self.deletage respondsToSelector:@selector(orderFormListHeaderViewMainBtnClick:)]) {
        [self.deletage performSelector:@selector(orderFormListHeaderViewMainBtnClick:)withObject:[NSString stringWithFormat:@"%ld",(long)self.section]];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
